package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class TwinsUnionRank extends BaseRepair {

    public TwinsUnionRank(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    //触发礼包一条龙三个日志
    public void go1(int activityId){
        List<Map<String, String>> resList = new ArrayList<>();
        List<Map<String, String>> listMap = new ArrayList<>();
        List<Map<String, String>> list2Map = new ArrayList<>();
        BaseMgr.getMgr(ActivityMgr.class).initActivity(activityId);
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo == null){
            System.err.println("没有找到活动");return;
        }
        if(!activityInfo.getServerIdList().contains(getGameServer().getServerInfo().getServerId())){
            System.err.println(activityId + "不是" + getGameServer().getServerInfo().getServerId()+"的活动");return;
        }
        List<ActivityConditionInfo> conditionList = ActivityMgr.getActivityConditionInfoByType(activityId,1248);
        String dateStart = DateHelper.getCurrentDateTimeString(activityInfo.getEndShowTime()*1000);
        String dateEnd = DateHelper.getCurrentDateTimeString((activityInfo.getEndShowTime() + 24 * 60 * 60)*1000);
        listMap = getMailUser(dateStart,dateEnd);
        list2Map = getMailLog(dateStart,dateEnd);
        listMap.addAll(list2Map);
        for(Map<String, String> data : listMap){
            String rankStr = getRankStr(data.get("annexes"),conditionList);
            if(!"0-0".equals(rankStr)){
                data.put("rank",rankStr);
                resList.add(data);
            }
        }
        DataMgr.addData(resList);
    }

    private String getRankStr(String annexesStr,List<ActivityConditionInfo> conditionList){
        for(ActivityConditionInfo conditionInfo : conditionList){
            if(conditionInfo.getRewardStr().equals(annexesStr) || conditionInfo.getMasterRewardStr().equals(annexesStr)){
                return conditionInfo.getParamList().get(0) + "-" + conditionInfo.getParamList().get(1);
            }
        }
        return "0-0";
    }

    private List<Map<String, String>> getMailUser(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT receiveUserId,annexes FROM t_u_mail " +
                " WHERE title = '绝代双骄活动奖励补发' AND " +
                " content = '亲爱的掌柜，您在绝代双骄活动中有奖励未能及时领取，现已补发，敬请查收！' " +
                " AND sendTime > '"+dateStart+"' AND sendTime < '"+dateEnd+"'";
        getLogger().error(sql);
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("receiveUserId")));
                temp.put("annexes",checkNullToEmpty(rs.getString("annexes")));
                temp.put("rank","0-0");
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getMailLog(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT receiveUserId,annexes FROM t_l_delete_mail " +
                " WHERE title = '绝代双骄活动奖励补发' AND " +
                " content = '亲爱的掌柜，您在绝代双骄活动中有奖励未能及时领取，现已补发，敬请查收！' " +
                " AND sendTime > '"+dateStart+"' AND sendTime < '"+dateEnd+"'";
        getLogger().error(sql);
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            Map<String,Map<String,String>> resMap = new ConcurrentHashMap<>();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("receiveUserId")));
                temp.put("annexes",checkNullToEmpty(rs.getString("annexes")));
                temp.put("rank","0-0");
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

}
